// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIBinaryConfigurationFieldEditor.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    using Org.OpenTrader.Framework.LiveObjects;

    #endregion

    /// <summary>
    /// The ui configuration resource.
    /// </summary>
    public partial class UIBinaryConfigurationFieldEditor : UIControl
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIBinaryConfigurationFieldEditor"/> class. 
        /// Initializes a new instance of the <see cref="UIBinaryConfigurationField"/> class.
        /// </summary>
        public UIBinaryConfigurationFieldEditor()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIBinaryConfigurationFieldEditor"/> class. 
        /// Initializes a new instance of the <see cref="UIBinaryConfigurationField"/> class.
        /// </summary>
        /// <param name="resource">
        /// The resource.
        /// </param>
        public UIBinaryConfigurationFieldEditor(BinaryConfigurationField resource)
        {
            this.InitializeComponent();
            this.Resource = resource;

            if (!resource.IsMandatory)
            {
                this.unsetButton.Enabled = resource.IsSet;
            }
            else
            {
                this.unsetButton.Enabled = false;
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// The user interaction done cb.
        /// </summary>
        public event EventHandler<EventArgs> UserInteractionDoneCB;

        #endregion

        #region Properties

        /// <summary>
        /// Gets Resource.
        /// </summary>
        public BinaryConfigurationField Resource { get; private set; }

        #endregion

        #region Methods

        /////// <summary>
        /////// The exportButton_ click.
        /////// </summary>
        /////// <param name="sender">
        /////// The sender.
        /////// </param>
        /////// <param name="e">
        /////// The e.
        /////// </param>
        ////private void OnExportButtonClick(object sender, EventArgs e)
        ////{
        ////    this.savefiledialog.FileName = this.Resource.Name;
        ////    var r = this.savefiledialog.ShowDialog();
        ////    if (r == DialogResult.OK)
        ////    {
        ////        try
        ////        {
        ////            var path = this.savefiledialog.FileName;

        ////            File.WriteAllBytes(path, (byte[])this.Resource.Contents);
        ////        }
        ////        catch (Exception ex)
        ////        {
        ////            MessageBox.Show(ex.ToString(), "Unable to export ...");
        ////        }
        ////    }

        ////    if (this.UserInteractionDoneCB != null)
        ////    {
        ////        this.UserInteractionDoneCB(this, new EventArgs());
        ////    }
        ////}

        /// <summary>
        /// The importButton_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnImportButtonClick(object sender, EventArgs e)
        {
            var r = this.openfiledialog.ShowDialog();
            if (r == DialogResult.OK)
            {
                try
                {
                    var path = this.openfiledialog.FileName;
                    var dir = Path.GetDirectoryName(path);
                    var name = Path.GetFileName(path);

                    var mm = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(name));
                    var sr = new StreamReader(mm);
                    var status = this.Resource.DeSerialize(sr, new SerializationContext(dir));
                    if (status)
                    {
                        this.Resource.Name = name;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Unable to import ...");
                }
            }

            if (this.UserInteractionDoneCB != null)
            {
                this.UserInteractionDoneCB(this, new EventArgs());
            }
        }

        /// <summary>
        /// The clearButton_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnUnsetButtonClick(object sender, EventArgs e)
        {
            this.Resource.Unset();

            if (this.UserInteractionDoneCB != null)
            {
                this.UserInteractionDoneCB(this, new EventArgs());
            }
        }

        #endregion
    }
}